﻿using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Timers;

using Ramparts.Particles;


namespace Ramparts
{
    class BattlePhase : Scene
    {

        GraphicTimer countdownTimer;
        List<Cannon> cannons = new List<Cannon>();

        public BattlePhase(SceneManager sceneManager)
        {
            this.sceneManager = sceneManager;
        }

        public override bool initScene()
        {

            base.initScene();
            sceneManager.rampart.IsMouseVisible = true;

            viewportRect = new Rectangle(0, 0,
                                                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Width,
                                                sceneManager.rampart.graphics.GraphicsDevice.Viewport.Height);
            camera.ManualCamera = false;
            camera.Position = new Vector2(-1 * viewportRect.Width / 2, -1 * viewportRect.Height / 2);
            previousMouseState = Mouse.GetState();
            countdownTimer = new GraphicTimer(new Vector2(viewportRect.Width / 2, viewportRect.Height), sceneManager, 30, -1, 0);
            sceneManager.world.setIso();
            for (int x = 0; x < sceneManager.world.wallLayer.gridWidth; x++)
            {
                for (int y = 0; y < sceneManager.world.wallLayer.gridHeight; y++)
                {
                    if (sceneManager.world.buildingLayer.getGridValueAt(x, y) == 5 &&
                        sceneManager.world.wallLayer.getGridValueAt(x, y) > 50)
                    {
                        cannons.Add(new Cannon(new Vector2(x * 20 + 10, y * 20 + 10), sceneManager, useCamera));

                    }
                }

            }
                
            return true;

        }


        public override void drawScene()
        {

            base.drawScene();
            sceneManager.world.Draw();
            drawComponents();

            sceneManager.rampart.spriteBatch.End();
            sceneManager.rampart.spriteBatch.Begin();
            sceneManager.drawWorldDebug();
            countdownTimer.Draw();
            foreach (Cannon cannon in cannons)
            {
                cannon.cannonBall.Draw();

            }
            sceneManager.rampart.spriteBatch.End();

        }

        protected override void processMouseEvents(GameTime gameTime)
        {
            base.processMouseEvents(gameTime);
            
            bool fired = false;
            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
            {
                foreach (Cannon cannon in cannons)
                {
                    if (!cannon.cannonBall.alive && fired == false)
                    {
                        cannon.cannonBall.Fire(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
                        fired = true;
                    }

                }
            }

            previousMouseState = currentMouseState;
        }

        protected override void processKeyboardEvents(GameTime gameTime)
        {
            base.processKeyboardEvents(gameTime);
            currentKeyboardState = Keyboard.GetState();
            previousKeyboardState = currentKeyboardState;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            foreach (Cannon cannon in cannons)
            {
                cannon.cannonBall.Update();
            }
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

    }
}
